<template>
  <div class="resource-center">
    <!-- 主体内容区域 -->
    <div class="main-layout">
      <!-- 左侧侧边栏 (三级菜单) -->
      <div class="sidebar">
        <div class="sidebar-header">
          <h3>{{ sidebarTitle }}</h3>
        </div>

        <div class="menu-container">
          <!-- 年级菜单 (第一级) -->
          <div
              v-for="grade in menuData"
              :key="grade.gradeId"
              class="menu-item grade-item"
              :class="{ active: activeGrade === grade.gradeId }"
          >
            <div class="menu-main" @click="toggleGrade(grade)">
              <el-icon class="menu-icon"><Reading /></el-icon>
              <span class="menu-text">{{ grade.gradeName }}</span>
              <el-icon class="menu-arrow">
                <ArrowDown v-if="!grade.expanded" />
                <ArrowUp v-else />
              </el-icon>
            </div>

            <!-- 分类菜单 (第二级) -->
            <div v-if="grade.expanded" class="submenu">
              <div
                  v-for="category in grade.categories"
                  :key="category.id"
                  class="submenu-item category-item"
                  :class="{
                  active: activeCategory === category.id,
                  expanded: category.expanded
                }"
                  @click="toggleCategory(grade, category)"
              >
                <div class="category-main">
                  <el-icon class="submenu-icon"><component :is="getIconComponent(category.icon)" /></el-icon>
                  <span class="submenu-text">{{ category.name }}</span>
                  <el-icon v-if="category.subcategories?.length" class="submenu-arrow">
                    <ArrowRight v-if="!category.expanded" />
                    <ArrowDown v-else />
                  </el-icon>
                </div>

                <!-- 子分类菜单 (第三级) -->
                <div v-if="category.expanded && category.subcategories" class="sub-submenu">
                  <div
                      v-for="subcategory in category.subcategories"
                      :key="subcategory.id"
                      class="sub-submenu-item"
                      :class="{ active: activeSubcategory === subcategory.id }"
                      @click="selectSubcategory(grade, category, subcategory)"
                  >
                    <el-icon class="sub-submenu-icon"><Document /></el-icon>
                    <span class="sub-submenu-text">{{ subcategory.name }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧主内容区 -->
      <div class="main-content">
        <!-- 新增：搜索框区域 -->


        <ResourceContent
            v-if="activeSubcategory"
            :subcategoryId="activeSubcategory"
        />
        <div v-else class="placeholder">
          <p>请从侧边栏选择一个子分类</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import {
  Collection,
  Notebook,
  VideoPlay,
  DataAnalysis,
  Reading,
  Document,
  Setting,
  ArrowDown,
  ArrowUp,
  ArrowRight,
  Search  // 新增Search图标
} from '@element-plus/icons-vue';
import ResourceContent from '@/components/ResourceContent.vue';
import { ElMessage } from 'element-plus';
import { fetchTeachingMenu } from "@/api/teacher";

// 当前激活的选项卡
const activeTab = ref('all');

// 菜单数据
const menuData = ref<any[]>([]);
const activeGrade = ref<number | null>(null);
const activeCategory = ref<number | null>(null);
const activeSubcategory = ref<number | null>(null);

// 新增：搜索关键字
const searchKeyword = ref<string>('');


// 加载菜单数据
const loadMenuData = async () => {
  try {
    const res = await fetchTeachingMenu();
    if (res.success) {
      menuData.value = res.data.map((grade: any) => ({
        ...grade,
        expanded: false,
        categories: grade.categories?.map((category: any) => ({
          ...category,
          expanded: false
        })) || []
      }));
    } else {
      ElMessage.error(res.message || '加载菜单失败');
    }
  } catch (error) {
    ElMessage.error('加载菜单失败');
  }
};

// 图标映射
const getIconComponent = (iconName: string) => {
  const iconMap: { [key: string]: any } = {
    'Notebook': Notebook,
    'DataAnalysis': DataAnalysis,
    'Reading': Reading,
    'Setting': Setting,
    'VideoPlay': VideoPlay,
    'Document': Document
  };
  return iconMap[iconName] || Document;
};

// 切换年级展开状态
const toggleGrade = (grade: any) => {
  grade.expanded = !grade.expanded;
  activeGrade.value = grade.gradeId;

  // 折叠其他年级
  menuData.value.forEach(g => {
    if (g.gradeId !== grade.gradeId) {
      g.expanded = false;
    }
  });
};

// 切换分类展开状态
const toggleCategory = (grade: any, category: any) => {
  category.expanded = !category.expanded;
  activeCategory.value = category.id;

  // 折叠同一年级下的其他分类
  grade.categories.forEach((c: any) => {
    if (c.id !== category.id) {
      c.expanded = false;
    }
  });
};

// 选择子分类
const selectSubcategory = (grade: any, category: any, subcategory: any) => {
  activeSubcategory.value = subcategory.id;
  // 清空搜索关键字
  searchKeyword.value = '';
  console.log(`加载 ${grade.gradeName} - ${category.name} - ${subcategory.name} 的内容`);
};

// 侧边栏标题
const sidebarTitle = computed(() => {
  switch (activeTab.value) {
    case 'all': return '所有课程分类';
    case 'popular': return '热门推荐分类';
    case 'featured': return '精选课程分类';
    default: return '资源分类';
  }
});

// 组件挂载时加载数据
onMounted(() => {
  loadMenuData();
});
</script>

<style scoped>
.resource-center {
  padding: 10px;
  height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}

.button-tabs .el-button {
  border-radius: 16px;
  padding: 8px 20px;
}

/* 主体布局 */
.main-layout {
  display: flex;
  flex: 1;
  gap: 15px;
  overflow: hidden;
}

/* 侧边栏样式 */
.sidebar {
  width: 280px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.sidebar-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.menu-container {
  padding: 10px 0;
  flex: 1;
  overflow-y: auto;
}

/* 年级菜单项 (第一级) */
.grade-item {
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 1px solid #f0f0f0;
}

.grade-item:last-child {
  border-bottom: none;
}

.grade-item.active {
  background-color: #e8f4ff;
}

.menu-main {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  position: relative;
}

.menu-main:hover {
  background-color: #f5f7fa;
}

.menu-icon {
  margin-right: 10px;
  font-size: 18px;
  color: #409eff;
}

.menu-text {
  font-size: 14px;
  flex: 1;
  font-weight: 500;
}

.menu-arrow {
  font-size: 14px;
  color: #909399;
  transition: transform 0.3s;
}

/* 分类子菜单 (第二级) */
.submenu {
  background-color: #f9fafc;
  padding: 5px 0;
}

.category-item {
  cursor: pointer;
  transition: all 0.3s;
}

.category-main {
  display: flex;
  align-items: center;
  padding: 10px 15px 10px 35px;
  position: relative;
}

.category-main:hover {
  background-color: #f0f7ff;
}

.category-item.active .category-main {
  background-color: #e6f1ff;
  color: #409eff;
  font-weight: 500;
}

.submenu-icon {
  margin-right: 8px;
  font-size: 16px;
  color: #67c23a;
}

.submenu-text {
  font-size: 13px;
  flex: 1;
}

.submenu-arrow {
  font-size: 12px;
  color: #909399;
  transition: transform 0.3s;
}

/* 子分类菜单 (第三级) */
.sub-submenu {
  background-color: #f5f7fa;
  padding: 5px 0;
}

.sub-submenu-item {
  display: flex;
  align-items: center;
  padding: 8px 15px 8px 55px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 12px;
}

.sub-submenu-item:hover {
  background-color: #ecf5ff;
}

.sub-submenu-item.active {
  background-color: #d9ecff;
  color: #409eff;
  font-weight: 500;
}

.sub-submenu-icon {
  margin-right: 6px;
  font-size: 14px;
  color: #909399;
}

.sub-submenu-text {
  flex: 1;
}

/* 主内容区 */
.main-content {
  flex: 1;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
  padding: 20px;
  overflow: auto;
}

/* 新增：搜索区域样式 */
.search-section {
  margin-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 15px;
}

.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #909399;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .resource-center {
    padding: 8px;
    height: auto;
  }

  .button-tabs .el-button {
    padding: 6px 15px;
    font-size: 14px;
  }

  .main-layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    margin-bottom: 15px;
  }

  .category-main {
    padding-left: 25px;
  }

  .sub-submenu-item {
    padding-left: 40px;
  }

  /* 移动端搜索框宽度 */
  .search-section .el-input {
    width: 100% !important;
  }
}

/* 滚动条样式 */
.menu-container::-webkit-scrollbar {
  width: 4px;
}

.menu-container::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.menu-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.menu-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>
